// pages/user/complain/complain.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        complain: '',
        name: '',
        phone: '',
        order_no: '',
    },
    //获取投诉内容
    complainInput: function(e) {
        var complain = e.detail.value;
        var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
        this.setData({
            complain
        })
    },
    //获取姓名
    nameinput: function(e) {
        var name = e.detail.value;
        // var testName = handler.regTest(name, '请输入您的姓名');
        this.setData({
            name
        })
    },
    //获取电话号码
    phoneinput: function(e) {
        var phone = e.detail.value;
        // var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        this.setData({
            phone
        });
    },
    //确认投诉
    getComplain: function() {
        var that = this;
        var {
            complain,
            name,
            phone
        } = this.data;
        var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
        var testName = handler.regTest(name, '请输入您的姓名');
        var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        var allTest = testComplain && testName && testPhone;
        if (allTest) {
            app.request({
                url: api.user.complaints,
                method: 'post',
                data: {
                    order_no: that.data.order_no,
                    content: that.data.complain,
                    name: that.data.name,
                    phone: that.data.phone
                },
                success: function(rs) {
                    if (rs.code == 1) {
                        wx.redirectTo({
                            url: '../../map/orderGoing/orderGoing?order_no=' + that.data.order_no,
                        });
                    }else{
                      wx.showToast({  
                          title: rs.msg,  
                          icon: 'none',  
                          duration: 2000  
                      })  
                    }
                }
            })
        } else {
            var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
            var testName = handler.regTest(name, '请输入您的姓名');
            var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {}
})